﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace KTX
{
    public partial class fsodien : Form
    {
        private SqlConnection cnn;

        public fsodien()
        {
            InitializeComponent();
        }

        private void fsodien_Load(object sender, EventArgs e)
        {
            Configuration _config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            var strConnectionString = _config.ConnectionStrings.ConnectionStrings[1].ConnectionString;

            cnn = new SqlConnection(strConnectionString);

            //ẩn các textbox
            cmdsave.Enabled = false;
            txtid.Enabled = false;
            cmbroomid.Enabled = false;
            txtmonth.Enabled = false;
            txtoldnumber.Enabled = false;
            txtnewnumber.Enabled = false;
        }

        private void cmdload_Click(object sender, EventArgs e)
        {
            try
            {
                if (cnn.State == ConnectionState.Closed) cnn.Open();

                var cmd = new SqlCommand("select * from tb_Electricity", cnn);

                var tb = new DataTable();

                var result = cmd.ExecuteReader();

                tb.Load(result);

                dgv1.DataSource = tb;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void cmdadd_Click(object sender, EventArgs e)
        {
            cmdsave.Enabled = true;
            txtid.Enabled = true;
            cmbroomid.Enabled = true;
            txtmonth.Enabled = true;
            txtoldnumber.Enabled = true;
            txtnewnumber.Enabled = true;
            txtid.Focus();

            //đưa dữ liệu từ tb_Room vào RoomId
            if (cnn.State == ConnectionState.Closed) cnn.Open();

            var cmd = new SqlCommand("select * from tb_Electricity", cnn);

            var tb = new DataTable();

            tb.Load(cmd.ExecuteReader());

            this.cmbroomid.DataSource = tb;
            this.cmbroomid.DisplayMember = "RoomId";
            this.cmbroomid.ValueMember = "RoomId";
        }

        private void cmdexit_Click(object sender, EventArgs e)
        {
            cnn.Close();
            Application.Exit();
        }

        private void cmdsave_Click(object sender, EventArgs e)
        {
            try
            {
                if (cnn.State == ConnectionState.Closed) cnn.Open();
                var sql = string.Format("insert into  tb_Electricity(Id,RoomId,Month,OldNumber,NewNumber) values ({0},{1},{2},'{3}',{4},{5})", txtid.Text, cmbroomid.SelectedValue, txtmonth.Text, txtoldnumber.Text, txtnewnumber.Text);

                // Toan sua:
                //var sql = string.Format("insert into  tb_Electricity(RoomId,Month,OldNumber,NewNumber) values ({0},'{1}',{2},{3})", cmbroomid.SelectedValue, txtmonth.Text, txtoldnumber.Text, txtnewnumber.Text);

                var cmd = new SqlCommand(sql, cnn);
                cmd.ExecuteNonQuery();

                cmd = new SqlCommand("select * from tb_Electricity", cnn);
                var tb = new DataTable();
                tb.Load(cmd.ExecuteReader());

                dgv1.DataSource = tb;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
